package cn.sangyy.car.controller;

import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import cn.sangyy.car.query.CarTypeQuery;
import cn.sangyy.car.service.ICarTypeService;
import cn.sangyy.car.domain.CarType;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/carType")
public class CarTypeController {
    @Autowired
    public ICarTypeService carTypeService;

    /**
     * 保存和修改公用的
     * @param carType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CarType carType){
        try {
            if( carType.getId()!=null)
                carTypeService.updateById(carType);
            else
                carTypeService.insert(carType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carTypeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 通过id获取对象信息
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            CarType carType = carTypeService.selectById(id);
            return AjaxResult.me().setResultObj(carType);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有
    * @return
    */
    @GetMapping("/list")
    public AjaxResult list(){
        try {
            List<CarType> list = carTypeService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/listPage")
    public AjaxResult listPageByQuery(@RequestBody CarTypeQuery query) {
        Page<CarType> page = new Page<CarType>(query.getPage(),query.getRows());
        page = carTypeService.selectPage(page);
        PageList<CarType> pageList = new PageList<CarType>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
    * @author Sangyy
    * 插叙汽车类型树形
    */
    @GetMapping("/tree/{pid}")
    public AjaxResult tree(@PathVariable Long pid){
        List<CarType> typeList = carTypeService.tree(pid);
        return AjaxResult.me().setResultObj(typeList);
    }

    /**
    * @author Sangyy
    * 通过pid查询子类型
    */
    @GetMapping("/getChildren/{pid}")
    public AjaxResult getChildren(@PathVariable Long pid){
        List<CarType> carTypes = carTypeService.getChildren(pid);
        return AjaxResult.me().setResultObj(carTypes);
    }

    /**
    * @author Sangyy
    * 批量删除
    */
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody ArrayList<Long> ids){
        for (Long id : ids) {
            delete(id);
            System.out.println(id);
        }
        return AjaxResult.me();
    }
}
